'''并编写一个程序，读取一个python源代码文件，将文本中所有除替换保留字以外的小写字母换成大写字母，
生成后的文件要求能够被Python解释器正确执行。
'''

def readpy():
    f = open('tobeupper.py','r',encoding='utf-8')
    txt = f.readlines()
    f.close()
    return txt

def filter():   
    reservedWord = {' False', ' None', ' True', ' and ', 'as ',\
        'assert ', ' break', 'class ', ' continue','def ',\
        'del ', 'elif ', 'else', 'except', 'finally',\
        'for ', 'from ', 'global', 'if ', 'import',\
        ' in ', ' is ', 'lambda ', 'nonlocal ', ' not ',\
        ' or ', 'pass ', 'raise', 'return ', 'try ',\
        'while ', ' with ', 'yield '}
    funtionName = {'range(', 'append(', 'open(', 'write(', 'join(',\
        'close(', 'split(', 'uniform(', 'print(', 'strip(',\
                   'round(', 'str('}
    className = {'random'}
    staticParameter = {'\'int\'', '\'str\'', 'list', '\\n', 'encoding', 'utf-8', '\'r\'',  '\'w\''}
    filterRule = reservedWord | funtionName | className | staticParameter
    return filterRule

def upperword(ls,rule):
    newtxt = []
    for line in ls:
        i = 0
        filin = []
        for item in rule:
            if item in line:
                #print('{} is in the line'.format(item))
                filin.append(item)
                box = '{0[' + str(i) + ']}'
                line = line.replace(item,box)
                print(line)
                i += 1
        upline = line.upper()
        upline = upline.format(filin)
        newtxt.append(upline)
    return newtxt

def writefile(ls):
    f = open('upper.py','w',encoding='utf-8')
    for row in ls:
        f.write(row)
    f.close()

txt = readpy()
filterRule = filter()
newtxt = upperword(txt,filterRule)
writefile(newtxt)




